<?php

namespace app\mobile\controller;

use think\Controller;

use think\Request;
use think\Session;
use \think\Db;

use app\api\model\User as UserModel;

use app\mobile\extra\phpqucode;
use app\common\library\wechat\WxPayTwo;
use app\common\library\wechat\WxPay;
use app\common\library\wechat\WeChatPay;
use app\common\library\wechat\WxpayService;

//Session::set('openId', 'oHLxFwTgR1Ma3uBBzxdolJGgnXf0');
/*
http://www.ystmall.com/index.php/mobile/Award/lottery
http://www.ystmall.com/index.php/mobile/Promotion/activity?id=2
*/
class Award extends Base
{
    public $prizes = [
        1=>['id'=>1,'name'=>'一等奖','title'=>'送价值3880元 黄金超声炮一次','number'=>1],
        2=>['id'=>2,'name'=>'二等奖','title'=>'送价值1280元 黑金超光子一次','number'=>2],
        3=>['id'=>3,'name'=>'三等奖','title'=>'送价值580元舒敏之星一次','number'=>5],
        4=>['id'=>4,'name'=>'参与奖','title'=>'价值300元面部深度清洁水润养护','number'=>10],
    ];

    private function init(){
        $lottery_info = Db::name('lottery')->where('user_id',$this->user['user_id'])->find();
        if(!$lottery_info){
            $lottery_info = [
                'user_id'=>$this->user['user_id'],
                'open_id'=>$this->user['open_id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'status'=>0,
                'truename'=>$this->user['true_name'],
                'phone'=>$this->user['true_mobile'],
            ];
            $lottery_info['id'] =Db::name('lottery')->insertGetId($lottery_info);
        }
        return $lottery_info;
    }


	public function lottery(){
        $lottery_info = $this->init();

        //今日抽奖次数
        $gamed = 0;

        if(isset($lottery_info['status']) && $lottery_info['status']>0){
            $gamed = 1;
        }

        if(!isset($lottery_info['prize_name'])) $lottery_info['prize_name'] = '';

        $this->assign('gamed',$gamed);
        $this->assign('title','抽奖');
        $this->assign('lottery_info',$lottery_info);
		return $this->fetch('index');
	}


    public function get_prizes(){
        
        $prizes = $this->prizes + $this->prizes;

        foreach ($this->prizes as $key => $value) {
            $prizes[] = $value;
        }
        

        return json(['prizes'=>$prizes]);
    }

    /* 获取抽奖结果 */
    public function get_result(){
        $lottery_info = $this->init();
        if($lottery_info['status']>0){
            return json(['code'=>0,'msg'=>"您已抽中 《{￥lottery_info['name']}》，不要重复提交，同时请注意领奖通知！"]);
        }

        if($lottery_info['status']==-1 && $lottery_info['prize_name']){
            foreach ($this->prizes as $prize) {
                if ($prize['name'] == $lottery_info['prize_name'] ) {
                    return json(['code'=>1,'msg'=>$prize['name'],'item'=>$prize['id'],'gamed'=>1]);
                }
            }
        }

        $status =Db::name('lottery_config')->where('name','status')->value('value');
        //$status =1;
        if($status!=1){
            return json(['code'=>0,'msg'=>'抽奖活动尚未开始，敬请期待！']);
        }

        $all_user = Db::name('lottery')->count(); //所有shou'jiang

        $canyujiang =  $all_user-8;

        if($canyujiang <10){
            $this->prizes[4]['number'] = 10;
        }else{
            $this->prizes[4]['number'] = $canyujiang;
        }

        foreach($this->prizes as $key=>$vo){
             $count = Db::name('lottery')->where('prize_name',$vo['name'])->count();
            $this->prizes[$key]['number'] = $this->prizes[$key]['number']-$count;


            $draw = $this->drawLottery();
            Db::name('lottery')->where('id',$lottery_info['id'])->update([
                'status'=>-1,
                'prize_name'=>$draw['name'],
                'prize_info'=>$draw['title'],
                'create_time'=>date('Y-m-d H:i:s'),
            ]);

            return json(['code'=>1,'msg'=>$draw['name'],'item'=>$draw['id'],'gamed'=>1]);
        }
    }


    public function  save(){
        $lottery_info = $this->init();
        if($lottery_info['status']>0){
            return json(['code'=>0,'msg'=>"您已抽中 《{$lottery_info['prize_name']}》！<br>不要重复提交<br>同时请注意领奖通知！"]);
        }
        $name = request()->param('name');
        $phone = request()->param('phone');

        $lottery_info = $this->init();
        Db::name('lottery')->where('id',$lottery_info['id'])->update([
            'status'=>1,
            'truename'=>$name,
            'phone'=>$phone,
        ]);

        $data = [];
        if(!$this->user['true_name']){
            $data['true_name'] =$name;
        }

        if(!$this->user['true_mobile']){
            $data['true_mobile'] =$phone;
        }

        if(!empty($data)){
            Db::name('user')->where('user_id',$this->user['user_id'])->update($data);
        }

     
        return json(['code'=>1,'msg'=>"恭喜您抽中 《{$lottery_info['prize_name']}》！<br>请稍后注意领奖通知！"]);
        
    }

    private function drawLottery(){
        $data = [];
        foreach($this->prizes as $key=>$vo){
            if($vo['number']>0){
                for ($i = 0; $i < $vo['number']; $i++) {
                    $data[] = $vo;
                }
            }
        }
        $randomKey = array_rand($data);
        return $data[$randomKey];
    }


   
}